package com.nineclock.system.controller;

import com.nineclock.api.sysrem.OrganizationControllerApi;
import com.nineclock.attendance.dto.DepartmentOrUserSimpleDTO;
import com.nineclock.common.entity.PageResult;
import com.nineclock.common.entity.Result;
import com.nineclock.system.dto.SysCompanyUserDTO;
import com.nineclock.system.dto.SysDepartmentDTO;
import com.nineclock.system.service.SysCompanyUserService;
import com.nineclock.system.service.SysDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/organization")
public class OrganizationController implements OrganizationControllerApi {

    @Autowired
    private SysCompanyUserService sysCompanyUserService;

    @Override
    @GetMapping("/members/simple")
    public Result<List<SysCompanyUserDTO>> queryCompanyMemberList() {
       List<SysCompanyUserDTO> companyUserDTOS = sysCompanyUserService.queryCompanyMemberList();
        return Result.success(companyUserDTOS);
    }

    @Autowired
    private SysDepartmentService departmentService;

    /**
     * 查询组织部门列表
     * @return
     */
    @Override
    @GetMapping("/department")
    public Result<List<SysDepartmentDTO>> queryDepartmentList() {
        List<SysDepartmentDTO> sysDepartmentDTOS= departmentService.queryDepartmentList();
        return Result.success(sysDepartmentDTOS);
    }

    /**
     * 分页查询部门下的员工数据列表
     * @param page
     * @param pageSize
     * @param keyword
     * @param departmentId
     * @return
     */
    @Override
    @GetMapping("members")
    public Result<PageResult<SysCompanyUserDTO>> members(
            @RequestParam("page") Integer page,
            @RequestParam("pageSize")Integer pageSize,
            @RequestParam("keyword") String keyword,
            @RequestParam("departmentId") Long departmentId) {

        PageResult<SysCompanyUserDTO> companyUser=sysCompanyUserService.members(page,pageSize,keyword,departmentId);
        return Result.success(companyUser);
    }

    /**
     * 直接导入企业员工
     * @param excelFile
     * @return
     * @throws IOException
     */
    @Override
    @PostMapping("uploadExcel")
    public Result uploadExcel(MultipartFile excelFile) throws IOException {

        sysCompanyUserService.importData(excelFile);
        return Result.success();
    }

    /**
     * 查询企业ID下的所有顶级部门ID
     * @return
     */
    @Override
    @GetMapping("/department/top")
    public Result<List<Long>> queryDepartmentIds() {
        List<Long> ids=departmentService.queryDepartmentIds();
        return Result.success(ids);
    }

    /**
     * 根据部门ID, 查询该部门下的员工数量
     * @param departmentIds
     * @return
     */
    @Override
    @GetMapping("/departments/membernums")
    public Result<Integer> queryUserCountByDepartmentIds(@RequestParam("departmentIds") List<Long> departmentIds) {

        Integer count= sysCompanyUserService.queryUserCountByDepartmentIds(departmentIds);
        return Result.success(count);
    }

    /**
     *  根据部门ID查询子部门及员工列表
     * @param departmentId
     * @param includeMember
     * @return
     */
    @Override
    @GetMapping("/simple")
    public Result<List<DepartmentOrUserSimpleDTO>> querySimpleDepartment(@RequestParam(value = "id",required = false) Long departmentId,
                                                                         @RequestParam(value = "includeMember",required = false) Integer includeMember) {
        List<DepartmentOrUserSimpleDTO> departmentOrUserSimpleDTOS= departmentService.querySimpleDepartment(departmentId,includeMember);
        return Result.success(departmentOrUserSimpleDTOS);
    }

}
